﻿@{
    Layout = "Parent";    
 }
<div style="margin-left: 20px;">
    <div class="row">
        <ul class="nav nav-tabs">
            @foreach (var table in Model)
            {
                <li><a href="#@table.Id" data-toggle="tab">@table.Name</a></li>
            }
            <li><a href="#tablenames" data-toggle="tab">StaticTableName</a></li>
        </ul>
        <div class="tab-content">
            @foreach (var table in Model)
            {
                <div class="tab-pane" id="@(table.Id)">
                    <div class="accordion" id="accordion@(table.Id)">
                    <div class="accordion-group">
                            <div class="accordion-heading">
                                <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion@(table.Id)" href="#collapseZero@(table.Id)">
                                    Domain </a>
                            </div>
                            <div id="collapseZero@(table.Id)" class="accordion-body collapse">
                                <div class="accordion-inner">
                                    <div class="panel"><a href="###" onclick="copyToclip($(this).next().text());" style="position: absolute; top: 30px;right: 40px;">复制</a>
                                        <pre>   
    #region @table.DomainName
    
    public class @table.DomainName
    {
      @foreach (var column in table.Columns)
        {
        @:/// &lt;summary&gt;
        @:/// @column.Description
        @:/// &lt;/summary&gt;
        @:/// &lt;value&gt;@column.Default&lt;/value&gt;
        @:public @column.DataType @column.Name {get;set;}
        @:     
        }   
    }
               
    #endregion
    </pre>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="accordion-group">
                            <div class="accordion-heading">
                                <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion@(table.Id)" href="#collapseOne@(table.Id)">
                                    CURD </a>
                            </div>
                            <div id="collapseOne@(table.Id)" class="accordion-body collapse">
                                <div class="accordion-inner">
                                    <div class="panel"><a href="###" onclick="copyToclip($(this).next().text());" style="position: absolute; top: 30px;right: 40px;">复制</a>
                                        <pre>   
    #region @table.Name
    
    @(table.Curd(RazorSqlGenerator.Curd.Create))
    
    @(table.Curd(RazorSqlGenerator.Curd.Update))
    
    @(table.Curd(RazorSqlGenerator.Curd.Read))
    
    @(table.Curd(RazorSqlGenerator.Curd.Delete))
               
    #endregion
    </pre>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="accordion-group">
                            <div class="accordion-heading">
                                <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion@(table.Id)" href="#collapseTwo@(table.Id)">
                                    Interface </a>
                            </div>
                            <div id="collapseTwo@(table.Id)" class="accordion-body collapse">
                                <div class="accordion-inner">
                                    <div class="panel"><a href="###" onclick="copyToclip($(this).next().text());" style="position: absolute; top: 30px;right: 40px;">复制</a>
                                        <pre>   
    #region @table.DomainName
          
    [OperationContract]
    bool @(table.DomainName)Create(@(table.DomainName) o);
      
    [OperationContract]
    bool @(table.DomainName)Update(@(table.DomainName) o);
    
    [OperationContract]
    bool @(table.DomainName)Delete(@(table.GetPrimaryKeyType()) id);

    [OperationContract]
    @(table.DomainName) @(table.DomainName)Get(@(table.GetPrimaryKeyType()) id);
    
    [OperationContract]
    IList&lt;@(table.DomainName)&gt; @(table.DomainName)List();
    
    [OperationContract]
    PagedList&lt;@(table.DomainName)&gt; @(table.DomainName)PagedList(int page=1,int size=20);
    
    #endregion
    </pre>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="accordion-group">
                            <div class="accordion-heading">
                                <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion@(table.Id)" href="#collapseThree@(table.Id)">
                                    Service </a>
                            </div>
                            <div id="collapseThree@(table.Id)" class="accordion-body collapse">
                                <div class="accordion-inner">
                                    <div class="panel"><a href="###" onclick="copyToclip($(this).next().text());" style="position: absolute; top: 30px;right: 40px;">复制</a>
                                        <pre>   
    #region @table.DomainName
          
    public bool @(table.DomainName)Create(@(table.DomainName) o)
    {
      var template = new SqlBuilder()
      .Insert(Database.@(table.Database).Table.@(table.DomainName))
      .Values(@for(var i=0;i<table.Columns.Count();i++ ){var temp=table.Columns[i];if(temp.IsIdentity){continue;}else{<text>@(i==table.Columns.Count()-1?"o."+temp.Name+"":"o."+temp.Name+",")</text>}})
      .Complete();
      
      using (var connection = GetConnection(ConnectionName.@(table.Database)).EnsureOpen())
      {
        return connection.Execute(template.Sql, template.Param) == 1;
      }
    }
      
    public bool @(table.DomainName)Update(@(table.DomainName) o)
    {
      var template = new SqlBuilder()
      .Update(Database.@(table.Database).Table.@(table.DomainName))@{var cols=table.Columns.Where(o=>!o.IsIdentity && !o.IsPrimaryKey &&!RazorSqlGenerator.Table.IgnoreUpdateFields.Contains(o.Name)).ToList();var count=cols.Count;}@for(var i=0;i<count;i++ ){var temp=cols[i];<text>
      .Set("@temp.SafeName={0}",o.@temp.Name)</text>}
      
      .Where("[Id]={0}", o.Id).Complete();
      
      using (var connection = GetConnection(ConnectionName.@(table.Database)).EnsureOpen())
      {
        return connection.Execute(template.Sql, template.Param) == 1;
      }    
    }
    
    public bool @(table.DomainName)Delete(@(table.GetPrimaryKeyType()) id)
    {
      var template = new SqlBuilder()
      .Delete(Database.@(table.Database).Table.@(table.DomainName))
      .Where("[Id]={0}", id).Complete();
      
      using (var connection = GetConnection(ConnectionName.@(table.Database)).EnsureOpen())
      {
        return connection.Execute(template.Sql, template.Param) == 1;
      }
    }
    
    public @(table.DomainName) @(table.DomainName)Get(@(table.GetPrimaryKeyType()) id)
    {
      var template = new SqlBuilder()
      .Select("*")
      .From(Database.@(table.Database).Table.@(table.DomainName))
      .Where("[Id]={0}", id).Complete();
      
      using (var connection = GetConnection(ConnectionName.@(table.Database)).EnsureOpen())
      {
        return connection.Query&lt;@(table.DomainName)&gt;(template.Sql, template.Param).FirstOrDefault();
      }
    }
    
    public IList&lt;@(table.DomainName)&gt; @(table.DomainName)List()
    {
      var template = new SqlBuilder()
      .Select("*")
      .From(Database.@(table.Database).Table.@(table.DomainName))
      .Where("1=1").Complete();
      
      using (var connection = GetConnection(ConnectionName.@(table.Database)).EnsureOpen())
      {
        return connection.Query&lt;@(table.DomainName)&gt;(template.Sql, template.Param).ToList();
      }
    }    
    
    public PagedList&lt;@(table.DomainName)&gt; @(table.DomainName)PagedList(int page=1,int size=20)
    {
      var builder = new RegexSqlBuilder().Field("*")
      .Table(Database.@(table.Database).Table.@(table.DomainName))      
      .OrderBy("[Id] DESC")
      .AddParameters(new { begin = (page - 1) * size + 1, end = page * size });
      /*
      builder.Where("[Status]=@@Status", new { status });
      */
      var template = builder.Complete(RegexSqlTemplate.PagedSelect);
      using (var connection = GetConnection(ConnectionName.@(table.Database)).EnsureOpen())
      {
        return connection.PagedList&lt;@(table.DomainName)&gt;(template);
      }
    }
    
    #endregion
    </pre>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="accordion-group">
                            <div class="accordion-heading">
                                <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion@(table.Id)" href="#collapseFour@(table.Id)">
                                    RegexSqlBuilder Service </a>
                            </div>
                            <div id="collapseFour@(table.Id)" class="accordion-body collapse">
                                <div class="accordion-inner">
                                    <div class="panel"><a href="###" onclick="copyToclip($(this).next().text());" style="position: absolute; top: 30px;right: 40px;">复制</a>
                                        <pre>   
    #region @table.DomainName
          
    public bool @(table.DomainName)Create(@(table.DomainName) o)
    {
      var builder = new RegexSqlBuilder()
      .Table(Database.@(table.Database).Table.@(table.DomainName))
      .Field("(@for(var i=0;i<table.Columns.Count();i++ ){var temp=table.Columns[i];if(temp.IsIdentity){continue;}else{<text>@(i==table.Columns.Count()-1?temp.SafeName+"":temp.SafeName+",")</text>}})VALUES(@for(var i=0;i<table.Columns.Count();i++ ){var temp=table.Columns[i];if(temp.IsIdentity){continue;}else{<text>@(i==table.Columns.Count()-1?"@"+temp.Name+"":"@"+temp.Name+",")</text>}})")
      .AddParameters(new {@for(var i=0;i<table.Columns.Count();i++ ){var temp=table.Columns[i];if(temp.IsIdentity){continue;}else{<text>o.@(i==table.Columns.Count()-1?temp.Name+"":temp.Name+",")</text>}}});

      var template = builder.Complete(RegexSqlTemplate.Insert);
      using (var connection = GetConnection(ConnectionName.@(table.Database)).EnsureOpen())
      {
        return connection.Execute(template.Sql, template.Param) == 1;
      }
    }
      
    public bool @(table.DomainName)Update(@(table.DomainName) o)
    {
      var builder = new RegexSqlBuilder()
      .Table(Database.@(table.Database).Table.@(table.DomainName))
      .Field("@{var columns=table.Columns.Where(o=>!o.IsIdentity && !RazorSqlGenerator.Table.IgnoreUpdateFields.Contains(o.Name)).ToList();var len=columns.Count;}@for(var i=0;i<len;i++ ){var temp=columns[i];<text>@(i==len-1?temp.SafeName+"=@"+temp.Name+"":temp.SafeName+"=@"+temp.Name+",")</text>}")
      .AddParameters(new {@for(var i=0;i<len;i++ ){var temp=columns[i];<text>o.@(i==len-1?temp.Name+"":temp.Name+",")</text>}})
      .Where("[Id] = @@Id", new { o.Id });
      
      var template = builder.Complete(RegexSqlTemplate.Update);      
      using (var connection = GetConnection(ConnectionName.Passport))
      {
        return connection.Execute(template.Sql, template.Param) == 1;
      }
    }
    
    public bool @(table.DomainName)Delete(@(table.GetPrimaryKeyType()) id)
    {
      var builder = new RegexSqlBuilder()
      .Table(Database.@(table.Database).Table.@(table.DomainName))
      .Where("[Id] = @@Id", new { id });

      var template = builder.Complete(RegexSqlTemplate.Delete);
      using (var connection = GetConnection(ConnectionName.@(table.Database)).EnsureOpen())
      {
        return connection.Execute(template.Sql, template.Param) == 1;
      }
    }
    
    public @(table.DomainName) @(table.DomainName)Get(@(table.GetPrimaryKeyType()) id)
    {
      var builder = new RegexSqlBuilder()
      .Field("*")
      .Table(Database.@(table.Database).Table.@(table.DomainName))
      .Where("[Id] = @@Id", new { id });

      var template = builder.Complete(RegexSqlTemplate.SelectTopOne);
      using (var connection = GetConnection(ConnectionName.@(table.Database)).EnsureOpen())
      {
        return connection.Query&lt;@(table.DomainName)&gt;(template.Sql, template.Param).FirstOrDefault();
      }
    }
    
    public IList&lt;@(table.DomainName)&gt; @(table.DomainName)List()
    {
      var builder = new RegexSqlBuilder().Field("*")
      .Table(Database.@(table.Database).Table.@(table.DomainName))
      .Where("1=1");
      
      var template = builder.Complete(RegexSqlTemplate.Select);
      using (var connection = GetConnection(ConnectionName.@(table.Database)).EnsureOpen())
      {
        return connection.Query&lt;@(table.DomainName)&gt;(template.Sql, template.Param).ToList();
      }
    }
    
    public PagedList&lt;@(table.DomainName)&gt; @(table.DomainName)PagedList(int page=1,int size=20)
    {
      var builder = new RegexSqlBuilder().Field("*")
      .Table(Database.@(table.Database).Table.@(table.DomainName))
      .OrderBy("[Id] DESC")
      .AddParameters(new { begin = (page - 1) * size + 1, end = page * size });
      /*
      builder.Where("[Status]=@@Status", new { status });
      */
      var template = builder.Complete(RegexSqlTemplate.PagedSelect);
      using (var connection = GetConnection(ConnectionName.@(table.Database)).EnsureOpen())
      {
        return connection.PagedList&lt;@(table.DomainName)&gt;(template);
      }
    }
    
    #endregion
    </pre>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="accordion-group">
                            <div class="accordion-heading">
                                <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion@(table.Id)" href="#collapseFive@(table.Id)">
                                    Extra </a>
                            </div>
                            <div id="collapseFive@(table.Id)" class="accordion-body collapse">
                                <div class="accordion-inner">
                                    <div class="panel"><a href="###" onclick="copyToclip($(this).next().text());" style="position: absolute; top: 30px;right: 40px;">复制</a>
                                        <pre>
    var o=new @(table.DomainName)();   
    @for(var i=0;i<table.Columns.Count();i++ )
    {
      var temp=table.Columns[i];      
      <text>@("o."+temp.Name+"=;")</text>      
    }
    </pre>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            }
            <div class="tab-pane" id="tablenames">
                <div class="panel">
                    <pre>
    internal static class @Model.First().Database
    {
      internal static class Table
      {
        @foreach (var table in Model)
        {
        
            @:internal static readonly string @(table.DomainName)="@(table.SafeSchema).@(table.SafeName)";
              
        }                   
      }
    }
     </pre>
                </div>
            </div>
        </div>
    </div>
</div>
